package com.jinlejia.erp.ui.mine.adapter;


import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.freedom.yefeng.yfrecyclerview.YfListAdapter;
import com.freedom.yefeng.yfrecyclerview.YfSimpleViewHolder;
import com.jinlejia.erp.R;
import com.jinlejia.erp.utils.DateUtil;
import com.wang.avi.AVLoadingIndicatorView;

import java.util.ArrayList;
import java.util.Map;


/**
 * 办件进度（adapter）
 */

public class ProcessProAdapter extends YfListAdapter<Map<String,Object>> {

    private Context context;

    public ProcessProAdapter(Context context ,ArrayList<Map<String, Object>> data ) {
        super(data);
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateDataViewHolder(ViewGroup parent) {
        context = parent.getContext().getApplicationContext();
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_process_pro,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateEmptyViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty_layout, parent, false);
        TextView txt = (TextView) view.findViewById(R.id.id_tips);
        txt.setText("暂无数据");
        return new YfSimpleViewHolder(view);
    }

    @Override
    public void onBindDataViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
        ViewHolder holder = (ViewHolder) viewHolder;
        Map<String, Object> map = mData.get(position);
        if (map.containsKey("schedule")){
            if (map.get("schedule").toString().equals("1")){ //已完成
                holder.state.setVisibility(View.VISIBLE);
                String tmp = "预计完成于"+ DateUtil.getTime(map.get("expectTime").toString(),"yyyy-MM-dd")
                        .concat("<br>由")+ map.get("finishUser").toString() + "完成于" +DateUtil.getTime(null != map.get("finishTime")?map.get("finishTime").toString():"","yyyy-MM-dd")
                        .concat("<br>备注:<br>") + map.get("remark").toString();
                holder.content.setText(Html.fromHtml(tmp));
                Glide.with(context).load(R.mipmap.ic_progress_nol).override(50, 50).into(holder.icon);
            }else if (map.get("schedule").toString().equals("0")){//未完成
                holder.content.setText("预计完成于" + DateUtil.getTime(map.get("expectTime").toString(),"yyyy-MM-dd")+"");
                holder.state.setVisibility(View.GONE);
                Glide.with(context).load(R.mipmap.ic_circle).into(holder.icon);
            }
        }
        if (map.get("nowStep").toString().equals("1")){
            holder.state.setBackgroundResource(R.color.yellow);
            holder.title.setTextColor(context.getResources().getColor(R.color.yellow));
            Glide.with(context).load(R.mipmap.ic_progress_select).override(50, 50).into(holder.icon);
        }else {
            holder.state.setBackgroundResource(R.color.hintColor);
            holder.title.setTextColor(context.getResources().getColor(R.color.hintColor));
        }
        if (map.containsKey("stepName")){
            holder.title.setText(map.get("stepName").toString());
        }
        holder.itemView.setTag(mData.get(position));
    }

    @Override
    public RecyclerView.ViewHolder onCreateLoadingViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.loading_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateFooterViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.footer_loading, parent, false);
        return new FooterViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateErrorViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.error_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public void onBindFooterViewHolder(RecyclerView.ViewHolder holder, int position) {
        super.onBindFooterViewHolder(holder, position);
        if (mFooters.get(0).equals(R.string.is_all)) {
            ((FooterViewHolder) holder).txt.setText(R.string.is_all);
            ((FooterViewHolder) holder).txt.setVisibility(View.VISIBLE);
            ((FooterViewHolder) holder).progress.setVisibility(View.GONE);
        } else {
            ((FooterViewHolder) holder).txt.setVisibility(View.GONE);
            ((FooterViewHolder) holder).progress.setVisibility(View.VISIBLE);
        }
    }

    static final class FooterViewHolder extends RecyclerView.ViewHolder {
        TextView txt;
        AVLoadingIndicatorView progress;

        public FooterViewHolder(View itemView) {
            super(itemView);
            txt = (TextView) itemView.findViewById(R.id.txt);
            progress = (AVLoadingIndicatorView) itemView.findViewById(R.id.progress);
        }
    }

    private static final class ViewHolder extends RecyclerView.ViewHolder {

        TextView title;
        TextView content;
        TextView state;
        ImageView icon;

        public ViewHolder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.title);
            content = (TextView) itemView.findViewById(R.id.content);
            state = (TextView) itemView.findViewById(R.id.state);
            icon = (ImageView) itemView.findViewById(R.id.icon);
        }

    }
}
